package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServicelmpl  implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Override
    @Transactional
    public void saveWithFlavor(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //insert套餐
        setmealMapper.insert(setmeal);

        //insert套餐关联菜品
        setmealDTO.getSetmealDishes().forEach(s -> s.setSetmealId(setmeal.getId()));
        setmealDishMapper.insert(setmealDTO.getSetmealDishes());


    }
    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     */
    @Override
    public PageResult getSetMealPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page= setmealMapper.getSetMealPage(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }
    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    @Transactional //事务
    public void delete(List<Long> ids) {
        //判断当前套餐是否未起售
Integer count = setmealMapper.countStatus(ids);
        if (count >0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }

        //删除套餐
        setmealMapper.delete(ids);

        //删除套餐关联的菜品
        setmealDishMapper.delete(ids);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void revise(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改套餐
        setmealMapper.update(setmeal);

        //删除套餐关联菜品
        setmealDishMapper.deleteBySetMeaklId(setmealDTO.getId());

        //重新添加套餐关联的菜品
        setmealDTO.getSetmealDishes().forEach(s -> s.setSetmealId(setmealDTO.getId()));
        setmealDishMapper.insert(setmealDTO.getSetmealDishes());

    }


    /**
     * 根据ID查询套餐
     * @param id
     */

    @Override
    public SetmealVO getAll(Integer id) {
      SetmealVO setmealVO = new SetmealVO();
      Setmeal setmeal =setmealMapper.getAll(id);
BeanUtils.copyProperties(setmeal,setmealVO);
      List<SetmealDish> list = setmealDishMapper.getListBySetmealId(id);
      setmealVO.setSetmealDishes(list);

        return setmealVO;
    }

    /**
     * 套餐的起售、停售
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
     Setmeal setmeal = Setmeal.builder().status(status).id(id).build();
     setmealMapper.update(setmeal);

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }


}

